package org.ezplatform.office.meeting.setting.base.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.ezplatform.core.dao.jpa.JpaBaseDao;
import org.ezplatform.core.service.BaseService;
import org.ezplatform.office.meeting.setting.base.dao.BaseSetDao;
import org.ezplatform.office.meeting.setting.base.entity.BaseSet;
import org.ezplatform.office.meeting.setting.base.web.rest.MeetingQSWebApiController;
import org.ezplatform.util.DateUtils;
import org.ezplatform.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class BaseSetService extends BaseService<BaseSet, String>{
    
	private final static Logger logger = LoggerFactory.getLogger(BaseSetService.class);
	
    @Autowired
    private BaseSetDao dao;
    
    @Override
    protected JpaBaseDao<BaseSet, String> getEntityDao() {
        return dao;
    }

    /**
     * 查询全时信息有没有同步
     * 
     * @return
     */
    public List<Map> getQSInfo() {
    	logger.debug("-----getQSInfo----");
    	
        StringBuffer sql = new StringBuffer("select id from oa_meeting_qsuser");
        
        return super.findByListNativeQuery(sql.toString(), "");
    }
    /**
     * 查询基础设置数据
     * @param corpId
     * @return
     */
    public BaseSet getBaseSetInfo(String corpId) {
        BaseSet baseSet = new BaseSet();
        List<BaseSet> list =  this.dao.getBaseSetInfo(corpId);
        if(list!=null && list.size()>0){
            baseSet =  list.get(0);
        }
        return baseSet;
    }
    
    /**
     * 查询基础设置数据
     * @param corpId
     * @return
     */
    public List<Map<String,String>> getBaseSetList(String corpId) {
        List<Map<String,String>> list = new ArrayList<Map<String,String>>();
        BaseSet baseSet = new BaseSet();
        List<BaseSet> baseSetList = this.dao.getBaseSetInfo(corpId);
        if(baseSetList!=null && baseSetList.size()>0){
            baseSet =  baseSetList.get(0);
            String noticeType = baseSet.getNoticeType();
            if(StringUtils.isNotEmpty(noticeType)) {
            	String[] strArr = noticeType.split(",");
                for(String s : strArr){
                    Map<String,String> map = new HashMap<String,String>();
                    map.put("value", s);
                    map.put("text",getBaseSetTypeName(s));
                    list.add(map);
                }
            }
        }
        return list;
    }
    

    /**
     * 查询会议申请选中的会议提醒方式
     * @param request
     * @param noticeType
     * @return
     */
    public List<Map<String, String>> getBaseSetCheckList(HttpServletRequest request, String noticeType) {
        if(StringUtils.isNotEmpty(noticeType)){
            List<Map<String, String>> list = new ArrayList<Map<String,String>>();
            if(StringUtils.isNotEmpty(noticeType)){
                String[] strArr = noticeType.split(",");
                for(String s : strArr){
                    Map<String,String> map = new HashMap<String,String>();
                    map.put("value", s);
                    map.put("text",getBaseSetTypeName(s));
                    list.add(map);
                }
            }
            return list;
        }
        return null;
    }
    
    public String getBaseSetTypeName(String type){
        String name = "";
        if("0".equals(type)){
            name = "钉钉";
        }else if("1".equals(type)){
            name = "微信";
        }else if("2".equals(type)){
            name = "evoPortal";
        }
        return name;
    }
    
    /**
     * 获取全部用户账号
     * @return
     */
    public List<Map> getAllUserAcc() {
    	logger.debug("-----------------getAllUserAcc-------");
        StringBuffer sql = new StringBuffer("select id,login_name,user_name from sys_user "
        		+ " where is_delete=0 and is_dormant=0 "
        		+ " and id not in (select userId from oa_meeting_qsuser where userId is not null) ");
        
        return super.findByListNativeQuery(sql.toString(), "");
    }
}